#ifndef _TCP_SERVER_H
#define _TCP_SERVER_H
#include <uv.h>
#include <map>
#include <mutex>
#include "Session.h"
class CTCPServer;
typedef std::shared_ptr<CTCPServer> CTCPServerPtr;
typedef std::function<void(CTCPServer* svrPtr,SessionPtr session)> NewSessionCb;
class CTCPServer :public std::enable_shared_from_this <CTCPServer>
{
public:
	template<class C = CTCPServer> static CTCPServerPtr NewTCPSvr(uv_loop_t* loop){
		CTCPServerPtr svr(new CTCPServer(loop));		
		return svr;
	}
	CTCPServer(uv_loop_t* loop);
	~CTCPServer();
	void StartServer(char* ip,size_t port);
	void StopServer();
	void OnMsgCall(SessionReadCb cb){ _sReadcb =cb;}	
	static void  _OnNewSession(uv_stream_t *server, int status);
public:
	uv_loop_t* _loop = nullptr;
	uv_tcp_t* _server = nullptr;
	std::map<unsigned long long, SessionPtr> m_Mpsession;	 
	SessionReadCb _sReadcb=nullptr;
	NewSessionCb _newSeesionCb=nullptr;	
	size_t _conn_cnt=0;
	std::string _ip;
	size_t _port;
};
#endif

